/*
 * Netflix API Basics
 * Copyright (c) 2009 Dave Cook Consulting, LLC
 *                    http://www.netdave.com
 * 
 * This code released under the Code Project Open License
 * http://www.codeproject.com/info/cpol10.aspx
 * 
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using OAuth;

namespace NetflixCatalogExplorer
{
	class NetflixRequest : OAuthBase
	{
		private string _consumerKey;
		private string _consumerSecret;

		/// <summary>
		/// Initialize the request calls
		/// </summary>
		/// <param name="ConsumerKey"></param>
		/// <param name="ConsumerSecret"></param>
		public NetflixRequest(string ConsumerKey, string ConsumerSecret)
		{
			_consumerKey = ConsumerKey;
			_consumerSecret = ConsumerSecret;
		}

		/// <summary>
		/// Non-Authenticated Request
		/// </summary>
		/// <param name="requestUrl">Resource to query.</param>
		/// <returns>XmlDocument object containing the results of the request.</returns>
		public XmlDocument NonAuthRequest(string requestUrl)
		{
			// Rather than munge the original base class code, I've yoinked the bits I need to here
			List<QueryParameter> parameters = new List<QueryParameter>();
			parameters.Add(new QueryParameter(OAuthConsumerKeyKey, _consumerKey));
			if (_additionalParameters.Count > 0)
			{
				foreach (DictionaryEntry de in _additionalParameters)
				{
					parameters.Add(new QueryParameter(de.Key.ToString(), de.Value.ToString()));
				}
			}
			parameters.Sort(new QueryParameterComparer());

			// construct the REST request
			string reqUrl = requestUrl +
							"?" + NormalizeRequestParameters(parameters);
			return ServiceRequest(reqUrl);
		}

		/// <summary>
		/// Signed Request
		/// </summary>
		/// <param name="requestUrl">Resource to query.</param>
		/// <returns>XmlDocument object containing the results of the request.</returns>
		public XmlDocument SignedRequest(string requestUrl)
		{
			// outputs
			string normalizedUrl;
			string normalizedRequestParameters;

			// generate request signature
			string sig = GenerateSignature(new Uri(requestUrl),
										   _consumerKey, _consumerSecret,
										   null, null,	// token , tokenSecret (not used)
										   "GET", GenerateTimeStamp(), GenerateNonce(),
										   out normalizedUrl, out normalizedRequestParameters);
			// put it all together to construct the signed request
			string reqUrl = normalizedUrl +
							"?" + normalizedRequestParameters +
							"&oauth_signature=" + UrlEncode(sig);
			return ServiceRequest(reqUrl);
		}

		// internal method for common web requests
		// note that the caller handles all exceptions
		private XmlDocument ServiceRequest(string requestUrl)
		{
			WebRequest req = WebRequest.Create(requestUrl);
			WebResponse rsp = req.GetResponse();
			XmlDocument xDoc = new	XmlDocument();
			xDoc.Load(rsp.GetResponseStream());
			rsp.Close();
			return xDoc;
		}
	}
}
